/*
 * 文件名：CTMSInvoker.java
 * 版权：Copyright 2006-2011 AsiaInfo Tech. Co. Ltd. All Rights Reserved. 
 * 描述： CTMSInvoker.java
 * 修改人：齐鹏飞
 * 修改时间：2011-9-20
 * 修改内容：新增
 */
package com.fengxing.ams.web.common;

import java.lang.reflect.Method;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.cxf.jaxrs.JAXRSInvoker;
import org.apache.cxf.jaxrs.model.OperationResourceInfo;
import org.apache.cxf.message.Exchange;
import org.springframework.stereotype.Component;

import com.fengxing.ams.pub.util.LogUtil;
import com.fengxing.ams.pub.vo.RequestVO;

/**
 * 根据路由方法确定调用接口
 * <p>
 * 根据路由方法确定调用接口
 * <p>
 * 
 * <pre>
 * </pre>
 * 
 * @author 齐鹏飞
 * @version CTMS V100R001 2011-9-20
 * @since CTMS V100R001C01
 */
@Component
public class RestInterfaceInvoker extends JAXRSInvoker {
	/**
	 * Logger for this class
	 */
	private static final Log logger = LogFactory
			.getLog(RestInterfaceInvoker.class);

	/**
	 * 上下文管理
	 */
	private IContextManager contextManger;

	/**
	 * 设置contextManger
	 * 
	 * @param contextManger
	 *            要设置的contextManger
	 */
	@Resource
	public void setContextManger(IContextManager contextManger) {
		this.contextManger = contextManger;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Object invoke(Exchange exchange, Object request,
			Object resourceObject) {
		if (logger.isDebugEnabled()) {
			LogUtil.debug(logger, "invoke(Exchange, Object, Object) - start"); //$NON-NLS-1$
		}

		OperationResourceInfo ori = exchange.get(OperationResourceInfo.class);

		Method method = ori.getMethodToInvoke();

		String requestKey = ori.getClassResourceInfo().getServiceClass()
				.getName()
				+ '.' + method.getName();

		RequestVO requestVO = this.contextManger.getRequestInfo();

		requestVO.setMethodName(requestKey);

		Object returnObject = super.invoke(exchange, request, resourceObject);

		if (logger.isDebugEnabled()) {
			LogUtil.debug(logger, "invoke(Exchange, Object, Object) - end"); //$NON-NLS-1$
		}
		return returnObject;
	}
}
